var logger = require('smart-logger')(module),
_ = require('underscore'),
utils = require('../utils/utils'),
mongo = require('mongodb'),
request = require('request'),
PreProcessor = require('smart-models').PreProcessor,
conf = require('smart-conf')(),
util = require('util'),
traverse = require('traverse'),
connector = require('../utils/connector');

module.exports = {
  // ### list()
  // Used to list all the preprocessor
  // - **@param req {Object}** the request object set by express;
  // the authent module will set req.user, express will set the id of the
  // datasource to retrieve as req.params.id that is a part of the uri
  // - **@param res {Object}** the response object set by express to handle the
  // response
  // - **@param next {Callback}** the callback to call when an error occurs
  add : function(req, res, next){
    var body=req.body,
    id=req.params["id"],
    block={};
    block.data=body;

    request.post({url:"http://smartdata.atosworldline.com/api/datasources/"+id+"/datas" ,body:JSON.stringify(block) , headers : {"Content-Type":"application/json"} } ,function(e,r,b){
      if(e) return res.json({response:"error", message:"Error while adding the data" , log:JSON.stringify(e)});
      logger.debug("response : " , r); 
      if(r.statusCode === 200) {
        res.json({response:"ok" });
      }else{
        res.json({response:"error", message:"Error while adding the data, probably datasource"+id+" nout found" });
      }

    });   
  }
};

